# encoding    : utf-8
# author      : 伦裕如
# project     : auto
# file        : operate
# time        : 2024-06-28 下午5:29
import time

from selenium import webdriver
from selenium.webdriver.common.by import By


class Operate:
    def login_err(self, driver: webdriver.Chrome, username: str, password: str):

        # 跳转到登录页面
        driver.get("http://localhost:90/")
        # 填写用户名
        driver.find_element(By.ID, "username").send_keys(username)
        # 填写密码
        driver.find_element(By.CSS_SELECTOR, "input[type='password']").send_keys(password)
        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
        time.sleep(0.5)

        # 获取页面标题用来判断结果
        help_block = driver.find_elements(By.CLASS_NAME, "help-block")
        texts = []
        for block in help_block:
            texts.append(block.text)
        return texts

    def login(self, driver: webdriver.Chrome, username: str, password: str):

        # 进入网址
        driver.get("http://localhost:90/")

        # 获取页面元素
        # 填写用户名
        driver.find_element(By.ID, "username").send_keys(username)
        # 填写密码
        driver.find_element(By.CSS_SELECTOR, "input[type='password']").send_keys(password)
        # time.sleep(20)
        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
        time.sleep(0.5)

    def register(self, driver: webdriver.Chrome, username: str, password: str, confirm_password: str):
        """
        注册用户
        :param driver: 驱动
        :param username: 用户名
        :param password: 密码
        :param confirm_password: 确认密码
        :return:
        """

        # 前往指定网页
        driver.get("http://localhost:90")
        # 跳转到注册页面
        driver.find_element(By.PARTIAL_LINK_TEXT, "注册").click()
        time.sleep(0.5)

        # 填写注册信息注册用户
        driver.find_element(By.CSS_SELECTOR, "input#username").send_keys(username)
        driver.find_element(By.CSS_SELECTOR, "input#password").send_keys(password)
        driver.find_element(By.CSS_SELECTOR, "input#repassword").send_keys(confirm_password)
        driver.find_element(By.ID, "login_btn").click()
        time.sleep(0.5)

        # 判断结果是否符华预期
        if driver.title == "度小满理财-登录[内测版]":
            return driver.title
        else:
            help_block = driver.find_elements(By.CLASS_NAME, "help-block")
            texts = []
            for block in help_block:
                texts.append(block.text)
            return texts

    def insert_bank_card(self, driver: webdriver.Chrome, bank_name: str, card_id: str, card_type: int, cancel: bool):

        driver.find_element(By.PARTIAL_LINK_TEXT, "个人中心").click()
        time.sleep(0.5)
        driver.find_element(By.PARTIAL_LINK_TEXT, "银行卡管理").click()
        time.sleep(0.5)

        # 统计当前银行卡总数
        cards = driver.find_elements(By.CLASS_NAME, "col-sm-12")
        num1 = len(cards)

        # 点击新增按钮
        driver.find_element(By.ID, "bankCard_add_modal_btn").click()
        time.sleep(0.5)
        # 填写卡片信息
        driver.find_element(By.ID, "cardBank_add_input").send_keys(bank_name)
        if card_type == 1:
            driver.find_element(By.XPATH, "//div[@class='example-box']/label[1]").click()
        elif card_type == 2:
            driver.find_element(By.XPATH, "//div[@class='example-box']/label[2]").click()
        driver.find_element(By.ID, "cardNum_add_input").send_keys(card_id)
        # 是否新增
        if cancel:
            driver.find_element(By.CSS_SELECTOR, "button.btn.btn-default").click()
            time.sleep(2)
            return num1 == len(driver.find_elements(By.CLASS_NAME, "col-sm-12"))
        else:
            driver.find_element(By.ID, "bankCard_save_btn").click()
            time.sleep(2)
            return num1 + 1 == len(driver.find_elements(By.CLASS_NAME, "col-sm-12"))

    def delete_bank_card(self, driver: webdriver.Chrome, card_id: str, cancel: bool):

        driver.find_element(By.PARTIAL_LINK_TEXT, "个人中心").click()
        time.sleep(0.5)
        driver.find_element(By.PARTIAL_LINK_TEXT, "银行卡管理").click()
        time.sleep(0.5)

        # 统计当前银行卡总数
        cards = driver.find_elements(By.CLASS_NAME, "col-sm-12")
        num1 = len(cards)

        # 点击删除按钮        获取多个元素，因为系统BUG做出的妥协，银行卡号不唯一。
        driver.find_elements(By.XPATH, f"//button[@delete-name='{card_id}']")[0].click()
        time.sleep(0.5)
        # 是否删除
        if cancel:
            (driver.find_element(By.XPATH, "/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[2]")
             .click())
            time.sleep(2)
            return num1 == len(driver.find_elements(By.CLASS_NAME, "col-sm-12"))
        else:
            (driver.find_element(By.XPATH, "/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]")
             .click())
            time.sleep(2)
            return num1 - 1 == len(driver.find_elements(By.CLASS_NAME, "col-sm-12"))

    def update_bank_card(self, driver: webdriver.Chrome, c_id, bank_name: str, card_id: str,
                         card_type: int, cancel: bool):
        driver.find_element(By.PARTIAL_LINK_TEXT, "个人中心").click()
        time.sleep(0.5)
        driver.find_element(By.PARTIAL_LINK_TEXT, "银行卡管理").click()
        time.sleep(0.5)

        # 点击更新按钮
        driver.find_element(By.XPATH, "//button[@update-id='{}']".format(c_id)).click()
        time.sleep(0.5)
        # 填写卡片信息
        driver.find_element(By.ID, "cardBank_update_input").clear()
        driver.find_element(By.ID, "cardBank_update_input").send_keys(bank_name)
        if card_type == 1:
            driver.find_element(By.XPATH, "/html/body/div[1]/div/main/div/div/div[10]/div"
                                          "/div/div[2]/form/div[2]/div[1]/div/label[1]").click()
        elif card_type == 2:
            driver.find_element(By.XPATH, "/html/body/div[1]/div/main/div/div"
                                          "/div[10]/div/div/div[2]/form/div[2]/div[1]/div/label[2]").click()
        driver.find_element(By.ID, "cardNum_update_input").clear()
        driver.find_element(By.ID, "cardNum_update_input").send_keys(card_id)
        # 是否更新
        if cancel:
            (driver.find_element(By.XPATH, "/html/body/div[1]/div/main/div/div/div[10]/div/div/div[3]/button[1]")
             .click())
            time.sleep(2)
        else:
            driver.find_element(By.CSS_SELECTOR, "#bankCard_update_btn").click()
            time.sleep(2)

    def loans(self, driver: webdriver.Chrome, money, term, cancel: bool):
        # 页面跳转
        driver.find_element(By.PARTIAL_LINK_TEXT, "金融工具").click()
        time.sleep(0.5)
        driver.find_element(By.PARTIAL_LINK_TEXT, "安全网贷").click()
        time.sleep(0.5)

        # 填写贷款信息
        driver.find_element(By.CSS_SELECTOR, "input#amount").send_keys(money)
        driver.find_element(By.CSS_SELECTOR, "input#term").send_keys(term)
        driver.find_element(By.CSS_SELECTOR, "button#submit").click()
        time.sleep(0.5)

        # 是否取消贷款
        buttons = driver.find_elements(By.CSS_SELECTOR, ".jconfirm-buttons>button")
        if cancel:
            buttons[1].click()
        else:
            buttons[0].click()
        time.sleep(0.5)

    def buy_products(self, driver: webdriver.Chrome, function: str, product: str, pay_password: str,
                     cancel: bool):
        """
        购买产品 未完成
        :param driver: 驱动对象
        :param function: 购买产品种类
        :param product: 购买产品
        :param pay_password: 支付密码
        :param cancel: 取消交易
        :return:
        """
        # 选择
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
        time.sleep(0.5)
        driver.find_element(By.LINK_TEXT, function).click()
        driver.find_element(By.XPATH, f"//button[@buybtnname='{product}']").click()
        time.sleep(0.5)
        # 输入密码
        driver.find_elements(By.XPATH, '//*[@id="password"]')[1].send_keys(pay_password)
        if cancel:
            # 点击取消
            driver.find_element(By.CLASS_NAME, "layui-layer-btn1")
        else:
            # 点击确认
            driver.find_element(By.CLASS_NAME, "layui-layer-btn0").click()




































    def assertEqual(self, driver: webdriver.Chrome, result, expect, err_text):
        if expect != result:
            driver.save_screenshot(f"{err_text}.png")
            assert False
        else:
            assert True

    def assert_except_in_result(self, driver: webdriver.Chrome, err_text, expect, result):
        if expect not in result:
            driver.save_screenshot(f"{err_text}.png")
            assert False
        else:
            assert True


